package com.dq.domain.record;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;

import com.dq.domain.org.Org;
import com.dq.domain.setup.GiftGrantSet;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/11/20 14:34
 */
@Data
@Entity
@ApiModel("礼物发放记录")
public class GiftGrantRecord extends BaseEntity implements VO.ToVO {
    @ApiModelProperty("类别（员工/顾客）")
    @Enumerated(EnumType.ORDINAL)
    protected GrantCategory grantCategory;
    @ApiModelProperty("编号")
    protected String giftGrantNo;
    @ApiModelProperty("礼物发放设置")
    @ManyToOne
    protected GiftGrantSet giftGrantSet;
    @ApiModelProperty("员工/顾客")
    @ManyToOne
    protected SysUser sysUser;
    @ApiModelProperty("门店")
    @ManyToOne
    protected Org org;
    @ApiModelProperty("发放时间")
    protected Long grantTime;
    @ApiModelProperty("数量")
    protected Long grantNum;
    @ApiModelProperty("价值")
    protected BigDecimal costPrice;
    @ApiModelProperty("描述")
    protected String description;
    @ApiModelProperty("用户id")
    @Transient
    protected List<Long> userId = new ArrayList<>();

    public enum GrantCategory{
        staff("员工"),
        customer("顾客");
        String name;
        GrantCategory(String name){ this.name = name;}
    }


    @Data
    @ApiModel(value = "礼物发放记录视图")
    public static class Vo implements VO {
        protected Long id;
        @ApiModelProperty("类别（员工/顾客）")
        protected GrantCategory grantCategoryEN;
        @ApiModelProperty("类别（员工/顾客）")
        protected String grantCategoryCN;
        @ApiModelProperty("编号")
        protected String giftGrantNo;
        @ApiModelProperty("礼物发放设置")
        protected Long giftGrantSetId;
        @ApiModelProperty("礼物发放设置")
        protected String giftGrantSetName;
        @ApiModelProperty("员工/顾客")
        protected String cardNo;
        protected String userRealcName;
        protected Long staffUserId;
        @ApiModelProperty("门店")
        protected String orgName;
        protected Long orgId;
        @ApiModelProperty("发放时间")
        protected String grantTime;
        @ApiModelProperty("数量")
        protected Long grantNum;
        @ApiModelProperty("价值")
        protected BigDecimal costPrice;
        @ApiModelProperty("描述")
        protected String description;
        @ApiModelProperty("用户id")
        protected List<Long> userId = new ArrayList<>();


        @ApiModelProperty(value = "创建时间")
        private String createTime;
        @ApiModelProperty(value = "修改时间")
        private String lastModifyTime;
        @ApiModelProperty(value = "创建者id")
        private Long createUserId;
        @ApiModelProperty(value = "创建者name")
        private String createUserName;
        @ApiModelProperty(value = "修改者id")
        private Long updateUserId;
        @ApiModelProperty(value = "修改者name")
        private String updateUserName;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.getId();
        vo.grantCategoryEN = this.getGrantCategory();
        vo.grantCategoryCN = this.getGrantCategory().name;
        vo.giftGrantNo = this.getGiftGrantNo();
        vo.giftGrantSetId = this.getGiftGrantSet().getId();
        vo.giftGrantSetName = this.getGiftGrantSet().getCategoryName();
        vo.cardNo = this.getSysUser().getCardNo();
        vo.userRealcName = this.getSysUser().getUserRealcName();
        vo.orgId = this.getOrg().getId();
        vo.orgName = this.getOrg().getSimpleName();
        vo.grantTime = fmt(this.getGrantTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.grantNum = fmt(this.getGrantNum());
        vo.costPrice = fmt(this.getCostPrice());
        vo.description = fmt(this.getDescription());
        if(this.getUserId().size() > 0) {
            vo.userId = this.userId;
        }else {
            List<Long> userId = this.userId;
            userId.add(this.getSysUser().getId());
            vo.userId = userId;
        }

        vo.staffUserId = this.sysUser.getId();

        vo.createTime = TimeUtil.longToString(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.lastModifyTime = TimeUtil.longToString(this.lastModifyTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createUserId = this.createUser == null ? null : this.createUser.getId();
        vo.createUserName = this.createUser == null ? null : this.createUser.getUserRealcName();
        vo.updateUserId = this.updateUser == null ? null : this.updateUser.getId();
        vo.updateUserName = this.updateUser == null ? null : this.updateUser.getUserRealcName();

        return vo;
    }
}
